#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpSocket=new QTcpSocket(this);
    tcpSocket->connectToHost("192.168.1.103", 6000);
    // QTimer::singleShot(1000, [=]() {
    //     QString str="base0809";
    //     QString vl = "SSPRO["+str+"]\n";
    //     qDebug()<<vl;
    //     tcpSocket->write(vl.toUtf8());
    //     QTimer::singleShot(10000, [=]() {
    //         tcpSocket->disconnectFromHost();
    //     });
    // });
    connect(tcpSocket, qOverload<QAbstractSocket::SocketError>(&QTcpSocket::error), this, [=](QAbstractSocket::SocketError error){
        qDebug() << "error" << error << ":" << tcpSocket->errorString();
    });

}

Widget::~Widget()
{
    delete ui;
}

QByteArray Widget::getSendJsondata(QJsonObject obj)
{
    QByteArray headTemprature;
    headTemprature.resize(11);
    headTemprature[0] = '\xf4';headTemprature[1] = '\xa3';
    QByteArray sendDataglobalConfig = QJsonDocument(obj).toJson();
    headTemprature[2] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0xff000000) >> 24);
    headTemprature[3] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x00ff0000) >> 16);
    headTemprature[4] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x0000ff00) >> 8);
    headTemprature[5] = (uint8_t)((sendDataglobalConfig.size() + 11) & 0x000000ff);
    headTemprature[6] = 1;
    headTemprature[7] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0xff000000) >> 24);
    headTemprature[8] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x00ff0000) >> 16);
    headTemprature[9] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x0000ff00) >> 8);
    headTemprature[10] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x000000ff));
    sendDataglobalConfig  = headTemprature + sendDataglobalConfig;
    qDebug()<<"wt"<<sendDataglobalConfig;
    return sendDataglobalConfig;
}

void Widget::on_pushButton_clicked()
{
    // //进入工作状态
    // tcpSocket->write("SWSTA");

    //切换方案
    // QString str="base0809";
    // QString vl = "SSPRO["+str+"]\n";
    // qDebug()<<vl;
    // tcpSocket->write(vl.toUtf8());




    //重启相机
    QString vl = "SSPRO["+str+"]\n";
    qDebug()<<vl;
    tcpSocket->write(vl.toUtf8());
}

